package com.thread.example.controller;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Auther:
 * @Description:
 * @Date: 2020/1/6 17:51
 */
public class ThreadAndThread {

    public static void main(String[] args) {
        threadOne();
    }

    public static void threadOne() {
        ExecutorService executorService = new ThreadPoolExecutor(
                3, 4, 5, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(12),
                new ThreadPoolExecutor.AbortPolicy()
        );
        for (int i = 0; i < 3; i++) {
            executorService.execute(new Thread(new work()));
        }
        executorService.shutdown();
    }

    public static void threadTwo() {
        ExecutorService executorService = new ThreadPoolExecutor(
                3, 4, 5, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(12),
                new ThreadPoolExecutor.AbortPolicy()
        );
        for (int i = 0; i < 3; i++) {
            executorService.execute(new Thread(new work2()));
        }
        executorService.shutdown();
    }

    public static class work implements Runnable {
        @Override
        public void run() {
            //  System.out.println("1--" + Thread.currentThread().getName() + "---" + Thread.currentThread().getId());
            threadTwo();
        }
    }

    public static class work2 implements Runnable {
        @Override
        public void run() {
            try {
                System.out.println("2--" + Thread.currentThread().getName() + "---" + Thread.currentThread().getId());
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
